/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.CustomerServiceItem;

@JsonApiResourceRepository(CustomerServiceItem.class)
@Component
public class CustomerServiceItemRepository extends BaseRepository<CustomerServiceItem> {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private NursingService nursingService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(CustomerServiceItem.class);
	}

	@Override
	@JsonApiSave
	public CustomerServiceItem save(CustomerServiceItem item) {
		if (item.getId() == null) {
			List<CustomerServiceItem> temp = customerService.getCustomerServiceItemByName(item
					.getName());
			if (CommonConstants.CUSTOMER_SERVICE_ITEM_CODE_OTHER.equals(item.getCode())) {
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NO_DELETE.label(),
						null);
			}
			if (temp == null || temp.size() == 0) {
				dslJpaService.create(item);
			} else {
				item.setErrcode(4);// name
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(),
						null);
			}
		} else {
			if (item.getDelStatus() == 1) {// 删除
				// 判断 project nursinglevel item
				int deleteFlag = nursingService.checkServiceItemRelations(item);
				if (deleteFlag > 0) {
					item.setErrcode(8);// no delete
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NO_DELETE.label(),
							null);
				}
				// 居家如果有订单关联 不删除 9
				int deleteFlag2 = nursingService.checkServiceItemRelationsJujia(item);
				if (deleteFlag2 > 0) {
					item.setErrcode(9);// no delete
					throw new BusinessException(null, CommonConstants.ERROR_CODE.BED_USED.label(),
							null);
				}
				dslJpaService.update(item);
			} else {//
				int updateFlag = 1;
				CustomerServiceItem oldData = customerService.getCustomerServiceItemById(item
						.getId());
				if (oldData.getName().equals(item.getName())) {

				} else {
					List<CustomerServiceItem> temp = customerService
							.getCustomerServiceItemByName(item.getName());
					if (temp == null || temp.size() == 0) {

					} else {
						updateFlag = 0;
					}
				}

				if (updateFlag == 1) {
					dslJpaService.update(item);
				} else {
					item.setErrcode(4);// name重复
					throw new BusinessException(null,
							CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
				}
			}
		}
		return item;
	}

}
